package com.ruoyi.web.wuchuan.util;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.ruoyi.web.wuchuan.dto.*;

import java.io.*;
import java.util.HashMap;
import java.util.Map;


public class PDFutil {

    public static void pdfout(Map<String, Object> datemap, String templatePath, String newPDFPath) {
        PdfReader reader;
        FileOutputStream out;
        ByteArrayOutputStream bos;
        PdfStamper stamper;
        try {
            BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            out = new FileOutputStream(newPDFPath);// 输出流
            reader = new PdfReader(templatePath);// 读取pdf模板
            bos = new ByteArrayOutputStream();
            stamper = new PdfStamper(reader, bos);
            AcroFields form = stamper.getAcroFields();
            //文字类的内容处理
            form.addSubstitutionFont(bf);
            for (String key : datemap.keySet()) {
                String value = datemap.get(key).toString();
                form.setField(key, value);
            }

            stamper.setFormFlattening(true);// 如果为false，生成的PDF文件可以编辑，如果为true，生成的PDF文件不可以编辑
            stamper.close();
            Document doc = new Document();
            PdfCopy copy = new PdfCopy(doc, out);
            doc.open();
            PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
            copy.addPage(importPage);
            doc.close();

        } catch (IOException | DocumentException e) {
            System.out.println(e);
        }
    }

    public  void pdfouts( String templatePath, PdfPTable table) {
        Document document = new Document(PageSize.A4);
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(templatePath));
            document.addTitle("example of PDF");
            document.open();
            document.add(table);
        } catch (FileNotFoundException | DocumentException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }

    public static String spliceParam(PDFProperties pdfProperties, String tempPath, String pdfName, String pdfModulePath, String uuid) {
        Map<String, Object> map = new HashMap<>();
        BaseData baseData = pdfProperties.getBaseData();  //get baseData
        if (pdfProperties.getType().equals("2")) {
            map.put("stgnqbh", baseData.getBh());
            map.put("mj", baseData.getMj());
        }
        map.put("zbdjd", baseData.getJd());
        map.put("zbdwd", baseData.getWd());
        map.put("ssxzqy", baseData.getXzqmc());
        Qxtz qxtzData = pdfProperties.getQxtz();  //get climateData
        map.put("ljyl", qxtzData.getLjjy());
        map.put("lsnpjwd", qxtzData.getPjwd());
        map.put("lsnpjsd", qxtzData.getPjsd());
        map.put("lsnyxjw", qxtzData.getYxjw());
        map.put("lstpjrzss", qxtzData.getPjrzss());
        map.put("lsndfts", qxtzData.getDfts());
        map.put("lsnghts", qxtzData.getGhts());
        Trtz trzsData = pdfProperties.getTrtz(); //get soildetailData
        map.put("trlx", trzsData.getTrlx());
        map.put("trsd", trzsData.getTrsd());
        Dltz dldmData = pdfProperties.getDltz(); //get geographyData
        map.put("pd", dldmData.getPd());
        map.put("px", dldmData.getPx());
        map.put("hbgd", dldmData.getHbgd());
        map.put("styy", dldmData.getStyy());
        String pdfPath = tempPath +File.separator+ uuid + File.separator + pdfName;  //linxu 写法
//        String pdfPath = tempPath + uuid + File.separator + pdfName;  //windows 写法
        pdfout(map, pdfModulePath, pdfPath);
        return pdfPath;
    }

    /**
     * 获取一级标题
     */
     public PdfPCell getCell(String content, Font font, int cloums, int high, int b) {
        PdfPCell cell = new PdfPCell(new Phrase(content, font));
        cell.setColspan(cloums);
        cell.setFixedHeight(high); //默认30
        if (b == 1) {
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平居中
        } else if (b == 0) {
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);//设置水平居左
        } else {
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);//设置水平居右
        }
        return cell;
    }


    public Font getFont(int size, boolean isJc) {
        Font font = null;
        try {
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            font = new Font(bfChinese);
            font.setSize(size);
            if (isJc) font.setStyle(Font.BOLD);
        } catch (DocumentException | IOException e) {
            e.printStackTrace();
        }
        return font;
    }



}
